﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using Vbyte.SharpOrm.Config;
using Vbyte.SharpOrm;

namespace NUnitTest
{
    /// <summary>
    ///产品分类
    /// </summary>
    [Serializable]
    [BindTable("DefaultDB", "gw_General_ProductClass")]
    public class ProductClass : TableEntry, IEntryExtension<ProductClass>
    {
        /// <summary>
        /// 分类编号
        /// </summary>
        /// <value>The PCID.</value>
        [PrimaryKey]
        [TableItem("PC_ID")]
        public short PCID { get; set; }

        /// <summary>
        /// 分类名称
        /// </summary>
        [TableItem("PC_Name")]
        public string PCName { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        [TableItem("PC_OrderNum")]
        public short Order { get; set; }

        /// <summary>
        /// 规格说明
        /// </summary>
        [TableItem("PC_Info")]
        public string Description { get; set; }

        /// <summary>
        /// 添加时间
        /// </summary>
        [TableItem("PC_AddTime")]
        [ImportIgnore]
        public DateTime AddDateTime { get; set; }


        #region IEntryExtension<ProductClass> 成员

        /// <summary>
        /// 从扩展数据绑定
        /// </summary>
        /// <param name="entryArray">The entry array.</param>
        /// <returns></returns>
        public ProductClass SetWithExtension(DictionaryEntry[] entryArray)
        {
            string Roles = "";
            ProductClass pc = new ProductClass();
            OrmHelper.DataBind<ProductClass>(pc, entryArray,
                new OrmHelper.OnEntryMissingProperty(delegate(string propName, object propValue)
                {
                    if (propName == "Roles") Roles = propValue.ToString();
                }));

            if (pc.PCID == 0)
            {
                short pcid = Convert.ToInt16(OrmHelper.Insert(pc, true));
                pc.PCID = pcid;
            }
            else
            {
                //pc.Update();
                OrmHelper.Update(pc);
            }

            //Database.ExecuteNonQuery("Delete From GW_Product_AdminRoles Where PID = " + pc.PCID + " And Type = 0");
            //if (Roles.Trim() != string.Empty)
            //{
            //    foreach (string rid in Roles.Split(','))
            //    {
            //        Database.ExecuteNonQuery("InSert Into GW_Product_AdminRoles (PID,RID,Type) Values (" + pc.PCID + "," + rid + ",0)");
            //    }
            //}

            return pc;
        }

        /// <summary>
        /// 获取相关扩展数据
        /// </summary>
        /// <returns></returns>
        public DictionaryEntry[] GetExtenEntryArray()
        {
            List<DictionaryEntry> entryArray = new List<DictionaryEntry>();
            //entryArray.Add(new DictionaryEntry("Roles", Database.ExecuteDataTable("select RID From GW_Product_AdminRoles Where PID = " + this.PCID + " AND Type=0")
            //    .Join(",", ",", false)));
            return entryArray.ToArray();
        }


        public void RemoveExtension()
        {
            //Database.ExecuteNonQuery("Delete From GW_Product_AdminRoles Where PID = "
            //    + this.PCID + " And Type = 0");
        }

        #endregion
    }

}